/*
 * 创建日期 2009-12-14
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.grid.tag;

import  org.fantasy.common.grid.bean.ReportParam;
import  org.fantasy.common.util.StringUtil;

import java.lang.reflect.Method;

import javax.servlet.jsp.JspException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *  表格参数标签
 * @author: 王文成
 * @version: 1.0
 * @since 2009-12-14
 */
public class GridParamTag extends AbstractTag {

    private static final Log log = LogFactory.getLog(GridParamTag.class);

    private static final String PARAM_BEAN = "PARAM_BEAN";
    
    public GridParamTag(){
        log.debug(">>>>>>>>>>>>>>>>>>> New GridParamTag instance!");
    }

    @Override
    protected void init() {
        pageContext.setAttribute(PARAM_BEAN, new ParamBean());
    }

    public void setName(String name) {
        getParamBean().setName(name);
    }

    public void setValue(String value) {
        getParamBean().setValue(value);
    }

    @Override
    public int doStartTag() throws JspException {
        ParamBean bean = getParamBean();
        ReportParam param = getReportParam();
        String methodName = "set" + StringUtil.firstCharUpperCase(bean.getName());
        Class[] params = new Class[] { String.class };
        Object[] args = new Object[] { bean.getValue() };
        try {
            Method method = param.getClass().getMethod(methodName, params);
            method.invoke(param, args);
        } catch (Exception e) {
            log.error(e);
            throw new JspException("无效的参数[name=" + bean.getName() + ";value=" + bean.getValue() + "]", e);
        }
        return EVAL_PAGE;
    }

    private ReportParam getReportParam() {
        return (ReportParam) pageContext.getAttribute(AbstractReportTag.REPORT_PARAM);
    }

    public ParamBean getParamBean() {
        return (ParamBean) pageContext.getAttribute(PARAM_BEAN);
    }

    private static class ParamBean {
        
        ParamBean(){
            log.debug(">>>>>>>>>>>>>>>>>>> New ParamBean instance!");
        }
        
        private String name;

        private String value;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}